package org.example.config.sequence.configs.processor.bean;

import org.example.config.sequence.configs.beans.MyBeanPostProcessorBean;
import org.example.config.sequence.inspect.Inspector;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 通过{@link Component @Component},{@link Bean @Bean}等注解声明方式加载，也可通过注册{@link BeanDefinition}方式加载
 * <p>
 * 本示例中通过{@link Component @Component}声明方式加载
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (beanName.startsWith("myBeanPostProcessorBean")) {
            Inspector.log(MyBeanPostProcessor.class, "postProcessBeforeInitialization");
            ((MyBeanPostProcessorBean) bean).setProperty1("prop1 in Before postProcessBeforeInitialization");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (beanName.startsWith("myBeanPostProcessorBean")) {
            Inspector.log(MyBeanPostProcessor.class, "postProcessAfterInitialization");
            ((MyBeanPostProcessorBean) bean).setProperty1("prop1 in After postProcessAfterInitialization");
        }
        return bean;
    }
}
